/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kh.pi.Main;

import com.kh.pi.Layout.LayoutBundle;
import com.kh.pi.Layout.PlayerController;
import com.kh.pi.Log.Log;
import com.kh.pi.MediaView.PiMediaView;
import java.net.URL;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.media.*;
import javafx.stage.Stage;
 
import com.kh.pi.Playlist.PlayList;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
 
public class PiPlayer extends Application {
 
    private LayoutBundle layoutBundle;
    public static void main(String[] args) {
        
        launch(args);
    }
 
    @Override
    public void start(final Stage stage) throws Exception {
        com.kh.pi.Log.Log logMgr=new Log();
        //variable
        final PlayerController controller;
        final MediaView mediaView;
        Parent root;
        URL location = getClass().getClassLoader().getResource("com/kh/pi/Resources/player.fxml");
        final List<MediaPlayer> playlist;
        
        //Object
        
        //load fxmlLoader
        try{
                //URL location = getClass().getClassLoader().getResource("com/khanhhoi/piplayer/Resources/player.fxml");
                layoutBundle=new LayoutBundle(location);
            
                //create Root 
                root = layoutBundle.getParent();       
                //create Controller
                controller=layoutBundle.getPlayerController();
            
            
                PlayList plistMgr=new PlayList(this.getHostServices().getDocumentBase(),"playlist/plist1.xml");
                playlist = plistMgr.getPlayList();
                
                PiMediaView piMV=new PiMediaView(playlist);
                mediaView=  piMV.getMediaView();
                for(int i = 0; i<playlist.size();i++){
                    final MediaPlayer currentPlayer     = playlist.get(i);
                    final MediaPlayer nextPlayer = playlist.get((i + 1) % playlist.size());
                    currentPlayer.setOnEndOfMedia(new Runnable() {
                        @Override
                        public void run() {
                            mediaView.setMediaPlayer(nextPlayer);
                            nextPlayer.play();
                            
                            //set screen
                            layoutBundle.setLayout(mediaView.getMediaPlayer().getMedia().getWidth(),
                                    mediaView.getMediaPlayer().getMedia().getHeight(), 
                                    mediaView.getFitWidth(),
                                    mediaView.getFitHeight(), 
                                    stage.getWidth(),
                                    stage.getHeight());
    
                            //Put code to check online here//get update when file running
                            
                        }
                    });
                }
                controller.setMediaViewToStackPane(mediaView);

                Scene scene =new Scene(root);
                scene.setFill(Color.BLACK);
                //Add css file
                scene.getStylesheets().add("com/khanhhoi/piplayer/Resources/buttons.css");
                //scene.getStylesheets().add("com/khanhhoi/piplayer/Resources/style.css");
                stage.setScene(scene);
                stage.setTitle("PiPlayer");
                stage.setFullScreen(true);
                stage.show();
                       
        //stage.
        mediaView.setOnMouseClicked(new EventHandler<MouseEvent>(){
            @Override
            public void handle(MouseEvent mouseEvent) {
                if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
                    if(mouseEvent.getClickCount() == 2){
                        if(stage.isFullScreen()){
                        stage.setFullScreen(false);}
                        else{stage.setFullScreen(true);}
                    }else
                    if(mouseEvent.getClickCount() == 1){
                        if(mediaView.getMediaPlayer().getStatus()==MediaPlayer.Status.PLAYING){
                            mediaView.getMediaPlayer().pause();
                        }else{
                            mediaView.getMediaPlayer().play();
                        }
                    }
                }
            }
        
        });

       mediaView.getMediaPlayer().setOnReady(new Runnable() {
            @Override
           public void run() {
                layoutBundle.setLayout(mediaView.getMediaPlayer().getMedia().getWidth(),
                    mediaView.getMediaPlayer().getMedia().getHeight(), 
                    mediaView.getFitWidth(),
                    mediaView.getFitHeight(), 
                    stage.getWidth(),
                    stage.getHeight());
                //Put code to check online here//get update when file running
            }
       });
       scene.widthProperty().addListener(new ChangeListener<Number>() {
            @Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight, Number newSceneHeight) {
                int w = mediaView.getMediaPlayer().getMedia().getWidth();
                int h = mediaView.getMediaPlayer().getMedia().getHeight();             
                //double y= (stage.getHeight()-h)/2;
                //controller.setTopStackPane(y);
                stage.setMinHeight(h);
                stage.setMinWidth(w);
                //controller.setTopStackPane(0);
                //controller.setLeftStackPane(0);
            }
        });
        scene.heightProperty().addListener(new ChangeListener<Number>() {
            @Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight, Number newSceneHeight) {
                int w = mediaView.getMediaPlayer().getMedia().getWidth();
                int h = mediaView.getMediaPlayer().getMedia().getHeight();             
                //double y= (stage.getHeight()-h)/2;
                //controller.setTopStackPane(y);
                stage.setMinHeight(h);
                stage.setMinWidth(w);
                //controller.setTopStackPane(0);
                //controller.setLeftStackPane(0);
            }
        });
      
       
        }catch(Exception e){
            e.printStackTrace();
            //logMgr.WriteException("Main exception: "+e.getStackTrace());
        }


    }
}